import Cocoa
import FlutterMacOS
import UserNotifications

@main
class AppDelegate: FlutterAppDelegate,UNUserNotificationCenterDelegate {

    

    override func applicationDidFinishLaunching(_ notification: Notification) {
        if let window = NSApplication.shared.windows.first {
            window.backgroundColor = NSColor.white
             // 将窗口大小设置为800x600
            window.setContentSize(NSMakeSize(800, 600))
            window.minSize = NSMakeSize(800, 600)
            // 获取屏幕的边框
            let screenFrame = window.screen?.frame ?? NSRect.zero
            //计算屏幕的中心点
            let centerX = (screenFrame.width - window.frame.width) / 2
            let centerY = (screenFrame.height - window.frame.height) / 2
            //将窗口的原点设置为中心点
            window.setFrameOrigin(NSPoint(x: centerX, y: centerY))
            window.title = "CowHorseXTool";
        }

        KeyboardTool.startListenKeyboard()
        MenuTool.shared.createMenu()
        AppBridgeTool.shared.createBridget(mainFlutterWindow: mainFlutterWindow)
        
        // 设置通知中心代理
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        
        // 请求授权
        center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
            if granted {
                print("通知授权成功")
            } else {
                print("通知授权失败: \(String(describing: error))")
            }
        }
    }
    
//    func userNotificationCenter(_ center: UNUserNotificationCenter,
//                                    willPresent notification: UNNotification,
//                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//            completionHandler([.alert, .sound])
//        }
   
    
  override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
    return true
  }

  override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
    return true
  }
}
